home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18515 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Return
  5. Date: 21 Apr 1996 05:10:29 GMT
  6. Organization: systems hk
  7. Message-ID: <4lcg05$64r@nadine.teleport.com>
  8. References: <4landb$345@news.nevada.edu>
  9. NNTP-Posting-Host: ip-pdx05-35.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. morrisd@nevada.edu (DAMON MORRIS) wrote:
  16. >I am having a really bad problem with some return statements...
  17. >I declare a function to return an int value then in the function
  18. >I check to see if two strings are equal and if so then I use another
  19. >variable to see what to return...
  20. >
  21. >int charisma(int ability, char *choice)
  22. > {
  23. >  if (choice == "Loyalty Base")
  24. >   {
  25. >    if (ability==10) return 20;
  26. >   }
  27. > }
  28. >
  29. >This little program will never work for some reason. When I call it
  30. >like this charisma(10,"Loyalty Base") it always returns some number
  31. >that it shouldn't. The number that is returned is usually between
  32. >5000-10000. If I wrote the value of choice to the screen before it
  33. >checked to see if it was equal to "Loyalty Base" it always says that
  34. >it is equal to "Loyalty Base", yet it never gets to the second if.
  35. >I have no idea what is the matter...I will send anyone full source if
  36. >needed to fix the problem, but the source is 1000+ lines....
  37. >
  38. Damon,
  39.  
  40. The problem in this piece of code is not with the returns,
  41. but rather with your string comparison.  In C/C++, you compare
  42. two strings (typically) with 'strcmp()'.  When you compare
  43. 'choice == "Loyalty Base"', you are comparing their 
  44. addresses, not their values:
  45.  
  46. Try instead:
  47.  
  48. int charisma(int ability, char *choice)
  49.  {
  50.   if (strcmp(choice,"Loyalty Base") == 0 )
  51.    {
  52.     if (ability==10) return 20;
  53.    }
  54.  }
  55.  
  56.  
  57. Yours, Geoff Houck
  58.  
  59.